import { Body, Controller, Get, Param, Post, Query, UseGuards } from '@nestjs/common';
import { SeasService } from './seas.service';
import { UseLog } from 'src/log/useLog.decorator';
import { ApiTags } from '@nestjs/swagger';
import { AuthGuard } from '@nestjs/passport';
import { UserGuard } from 'src/role/role.guard';
import { CurrentUser } from 'src/user/user.decorator';
import { UserSys } from 'src/user/userSys.entity';
import { Http, NotFound } from 'src/util/http';
import { Seas, SeasStatus, SeasType } from './seas.entity';
import { FindOptionsWhere, In } from 'typeorm';

@Controller('api/seas')
@ApiTags("公海库")
@UseGuards(AuthGuard("jwt"))
export class SeasController {
    constructor(
        private readonly seaService: SeasService
    ) { }

    @Get("get")
    @UseLog("获取公海库")
    @UseGuards(UserGuard())
    public get(
        @Query("status") status: SeasStatus,
        @Query("estimatedCategory") estimatedCategory: SeasType
    ) {
        const where: FindOptionsWhere<Seas> = {}
        if (!!status) where.status = status;
        if (!!estimatedCategory) where.estimatedCategory = estimatedCategory;
        return this.seaService.seas.find({
            order: {
                id: "desc"
            },
            where,
            relations: ["system", "user"]
        })
    }
    @Post(":status/:estimatedCategory/set")
    @UseLog("设置公海库状态、类别")
    @UseGuards(UserGuard())
    public set(
        @Body() { ids },
        @Param("status") status,
        @Param("estimatedCategory") estimatedCategory
    ) {
        return this.seaService.seas.update({
            id: In(ids)
        }, {
            estimatedCategory,
            status
        })
    }


    @Post("add")
    @UseLog("添加公海库")
    @UseGuards(UserGuard())
    public add(
        @CurrentUser() user: UserSys,
        @Body() {
            companyName,
            unifiedSocialCreditCode,
            location,
            estimatedCategory,
            rating,
            remark
        }
    ) {
        const entity = this.seaService.seas.create({
            companyName,
            unifiedSocialCreditCode,
            location,
            estimatedCategory,
            rating,
            user,
            system: user.system,
            remark
        });
        return this.seaService.seas.save(entity);
    }

    @Post(":id/data/upd")
    @UseLog("修改公海库")
    @UseGuards(UserGuard())
    public dataUpd(
        @Param("id") id: number,
        @Body() body
    ) {
        return this.seaService.seas.update({ id }, body)
    }

    @Post(":id/upd")
    @UseLog("修改公海库")
    @UseGuards(UserGuard())
    public upd(
        @Param("id") id: number,
        @Body() {
            companyName,
            unifiedSocialCreditCode,
            location,
            estimatedCategory,
            rating,
            remark
        }
    ) {
        return this.seaService.seas.update({ id }, {
            companyName,
            unifiedSocialCreditCode,
            location,
            estimatedCategory,
            rating,
            remark
        })
    }

    @Post("del")
    @UseLog("删除公海库")
    @UseGuards(UserGuard([]))
    public del(
        @Body() { ids }
    ) {
        return this.seaService.seas.delete(ids)
    }
}
